var http=require("express")();
var fs=require("fs");
// var query=require("querystring");
http.listen(6500,function () {
    console.log("服务器已开启，端口号6500")
});
http.use(function (req,res,next) {
    res.header("Access-Control-Allow-Origin","*");
    next();
});
//添加数据
http.get("/addmsg",function (req,res) {
    var dataObj=req.query;
    dataObj.id=(new Date()).getTime();
    fs.readFile("./data/shop.json","utf-8",function (err,data) {
        var arr=JSON.parse(data);
        arr.push(dataObj);
        fs.writeFile("./data/car.json",JSON.stringify(arr),"utf-8",function () {
            res.send("ok")
        })
    })
});
//删除数据
http.get("/delmsg",function(req,res){
    var dataObj = req.query;
    fs.readFile("./data/car.json","utf-8",function(err,data){
        var arr = JSON.parse(data);
        for (var i = 0 ; i < arr.length ; i++) {
            if (arr[i].id == dataObj.id) {
                arr.splice(i,1);
            }
        }
        fs.writeFile("./data/car.json",JSON.stringify(arr),"utf-8",function(){
            res.send("ok");
        })
    })
})

http.get("/findmsg",function(req,res){
    var dataObj=req.query;
    fs.readFile("./data/shop.json","utf-8",function(err,data){
        var arr=JSON.parse(data);
        for (var i=0;i<arr.length;i++) {
            if (arr[i].id==dataObj.id) {
                res.send(arr[i]);
            }
        }
    })
});
http.get("/addcarmsg",function(req,res){
    var dataObj = req.query;
    fs.readFile("./data/car.json","utf-8",function(err,data){
        var arr= JSON.parse(data);
        var bol = true;
        for (var i = 0 ; i < arr.length ; i++) {
            if (arr[i].id == dataObj.id) {
                arr[i].count = arr[i].count*1 + dataObj.count*1;
                bol = false;
            }
        }
        if (bol) {
            arr.push(dataObj);
        }
        fs.writeFile("./data/car.json",JSON.stringify(arr),"utf-8",function(){
            res.send("ok");
        })
    })
})
//改变数量
http.get("/changecount",function (req,res) {
    var dataObj=req.query;
    console.log(dataObj)
    fs.readFile("./data/car.json","utf-8",function (err,data) {
        var arr= JSON.parse(data);
        for(var i=0;i<arr.length;i++){
            if(arr[i].id==dataObj.id){
                if(dataObj.type=="+"){
                    arr[i].count=arr[i].count*1+1;
                }else if(dataObj.type=="-"){
                    if(arr[i].count<=0){
                        arr[i].count=1
                    }
                    arr[i].count=arr[i].count*1-1
                }
                var sendObj=arr[i]
                fs.writeFile("./data/car.json",JSON.stringify(arr),"utf-8",function () {
                    res.send(["ok"])
                })
            }
        }
    })
});
//验证手机号是否重复注册
http.get("/isrepeat",function (req,res) {
    var tel=req.query.tel;
    var bol=true;
    fs.readFile("./src/data/userlist.json","utf-8",function (err,data) {
        var arr=JSON.parse(data);
        for(var i=0;i<arr.length;i++){
            if(tel==arr[i].tel){
                bol=false;
                res.send("0")
            }
        }
        if(bol){
            res.send("1")
        }
    })
});
//验证码接口
http.post("/yzm",function (req,res) {
    parseInt(Math.random()*9);
    res.send(""+parseInt(Math.random()*9)+parseInt(Math.random()*9) +parseInt(Math.random()*9)+parseInt(Math.random()*9))
});
//校验码接口
http.post("/xym",function (req,res) {
    var dataStr="";
    req.on("data",function (data) {
        dataStr+=data;
    });
    req.on("end",function () {
        var dataObj=query.parse(dataStr);
        var bol=true;
        var num=""+parseInt(Math.random()*9)+parseInt(Math.random()*9)+parseInt(Math.random()*9)+parseInt(Math.random()*9)+parseInt(Math.random()*9) +parseInt(Math.random()*9);
    });
    fs.readFile("./src/data/xiaoyan.json","utf-8",function (err,data) {
        var arr=JSON.parse(data);
        for(var i=0;i<arr.length;i++){
            if(arr[i].tel==dataObj.tel){
                arr[i].xym=num;
                arr[i].time=dataObj.time;
                bol=false;
                fs.writeFile("./src/data/xiaoyan.json",JSON.stringify(arr),"utf-8",function () {
                    res.send(num);
                })
            }
        }
        if(bol){
            dataObj.xym=num;
            arr.push(dataObj);
            fs.writeFile("./src/data/xiaoyan.json",JSON.stringify(arr),"utf-8",function () {
                res.send(num);
            })
        }
    })
});
//注册
http.post("/adduser",function (req,res) {
    var dataStr="";
    req.on("data",function (data) {
        dataStr+=data;
    })
    req.on("end",function () {
        var dataObj=query.parse(dataStr);
        fs.readFile("./src/data/xiaoyan.json","utf-8",function (err,data) {
            var arr=JSON.parse(data);
            for(var i=0;i<arr.length;i++){
                if(dataObj.tel==arr[i].tel){
                    if(dataObj.time-arr[i].time>=15000){
                        res.send("0")
                    }else if(dataObj.xym==arr[i].xym){
                        fs.readFile("./src/data/xiaoyan.json","utf-8",function (err,data) {
                            var arr1=JSON.parse(data);
                            var obj={
                                id:dataObj.time,
                                user:dataObj.tel,
                                pass:dataObj.pwd
                            };
                            arr1.push(obj);
                            fs.writeFile("./src/data/userlist.json",JSON.stringify(arr1),"utf-8",function () {
                                res.send("1")
                            })
                        })
                    }else {
                        res.send("-1")
                    }
                }
            }
        })
    })
});
//登录
http.post("/dlmsg",function (req,res) {
    var dataStr="";
    req.on("data",function (data) {
        dataStr+=data;
    });
    req.on("end",function () {
        var dataObj=query.parse(dataStr);
        var bol=true;
        var num=""+parseInt(Math.random()*9)+parseInt(Math.random()*9)+parseInt(Math.random()*9)+parseInt(Math.random()*9)+parseInt(Math.random()*9)+parseInt(Math.random()*9)
    });
    fs.readFile("./data/xy.json","utf-8",function (err,data) {
        var arr=JSON.parse(data);
        for(var i=0;i<arr.length;i++){
            if(arr[i].tel==dataObj.tel){
                arr[i].xym=num;
                arr[i].time=dataObj.time;
                bol=false;
                fs.writeFile("./data/xy.json",JSON.stringify(arr),"utf-8",function () {
                    res.send(num);
                })
            }
        }
    })
});
http.get("/changecarmsg",function(req,res){
    var dataObj = req.query;
    fs.readFile("./data/car.json","utf-8",function(err,data){
        var arr = JSON.parse(data);
        for (var i = 0 ; i < arr.length ; i++) {
            arr[i].ischoose = dataObj.ischoose;
        }
        fs.writeFile("./data/car.json",JSON.stringify(arr),"utf-8",function(){
            res.send("ok");
        })
    })
});
http.get("/ischoosecarmsg",function(req,res){
    var dataObj = req.query;
    fs.readFile("./data/car.json","utf-8",function(err,data){
        var arr = JSON.parse(data);
        for (var i=0;i<arr.length;i++) {
            if (dataObj.id==arr[i].id) {
                arr[i].ischoose=dataObj.ischoose;
            }
        }
        fs.writeFile("./data/car.json",JSON.stringify(arr),"utf-8",function(){
            res.send("ok");
        })
    })
});
http.all("*",function (req,res) {
    res.sendFile(__dirname+req.url)
});
